using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CMS.Application.common.Interface;
using CMS.Application.Common.Interface;
using CMS.Application.RequestDto;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using SoftDemo.Api.Controllers;

namespace CMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]



    public class AttentionAuthorPage : ControllerBase
    {
        //关注表
        private readonly IRepository<AttentionAuthor> _attentionAuthor;
        private readonly IRepository<Users> _Users;
        private readonly IFileUploadService _fileUploadService;

        public AttentionAuthorPage(IRepository<AttentionAuthor> attentionAuthor, IFileUploadService fileUploadService, IRepository<Users> Users)
        {
            _attentionAuthor = attentionAuthor;
            _fileUploadService = fileUploadService;
            _Users = Users;
        }
        /// <summary>
        /// 关注页面返回数据
        /// </summary>
        /// <param name="useridDto"></param>
        /// <returns></returns>
        [HttpGet("/AttentionPage/{id}")]
        public async Task<Array> AttentionPage(Guid id)
        {

            var s = _attentionAuthor.Table.Where(x => x.UserId == id).ToList();
            //查询关注的id
            




            var UploadController = new UploadController(_fileUploadService);

            
            Object[] arr = new Object[s.Count];

            for (int i = 0; i < s.Count(); i++)
            {
                //所关注的用户
                var userId = await _Users.GetByIdAsync(s[i].AuthorId);
                
                if (userId != null)
                {
                    //关注用户的图片id
                    Guid csid = new Guid(userId.AvatarId.ToString());
                    // Guid csid = new Guid("1e931fc7-5af3-49bf-bedf-1a0c30abc22c");
                    //图片id获取图片
                    var result = await UploadController.GetFile(csid);
                    arr[i] = new
                    {
                        result.FileContents,
                        result.ContentType,
                        // AttentionId = user[i]
                        userId.UserName,
                        userId.Id
                    };
                }

            }
            return arr;
        }
    }
}